package pikes.peak;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.web.servlet.support.RequestContext;

import pikes.peak.ApplicationContextMessageProvider;
import pikes.peak.TextParameters;
import pikes.peak.test.PikesPeakComponentTestCase;
import pikes.peak.test.PikesPeakTestRequest;

public class ApplicationContextMessageProviderTestCase extends PikesPeakComponentTestCase {

	@Test
	public void simpleText() throws Exception {
		ApplicationContextMessageProvider text = new ApplicationContextMessageProvider("Some text");

		CharSequence actual = text.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertEquals("Translated Text", actual);
	}

	@Test
	public void simpleTextWithEmptyParameters() throws Exception {
		ApplicationContextMessageProvider text = new ApplicationContextMessageProvider("Some text",
				new TextParameters() {

					@Override
					protected void addParameters(List<Object> parameters, Map model, RequestContext requestContext)
							throws Exception {
					}

				});

		CharSequence actual = text.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertEquals("Translated Text", actual);
	}

	@Test
	public void simpleTextWithOneParameter() throws Exception {
		StaticMessageSource newMessageSource = new StaticMessageSource();
		newMessageSource.addMessage("Parameter Text Key", Locale.ENGLISH, "Parameter Text {0}");

		ApplicationContextMessageProvider text = new ApplicationContextMessageProvider("Parameter Text Key",
				new TextParameters() {

					@Override
					protected void addParameters(List<Object> parameters, Map model, RequestContext requestContext)
							throws Exception {
						parameters.add("boo");
					}

				});

		PikesPeakTestRequest pikesPeakRequest = new PikesPeakTestRequest(newMessageSource, themeMessageSource,
				localeResolver, themeResolver);

		CharSequence actual = text.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertEquals("Parameter Text boo", actual);
	}

	@Test
	public void simpleText1() throws Exception {
		ApplicationContextMessageProvider text = new ApplicationContextMessageProvider("Some text1");

		CharSequence actual = text.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertEquals("Some text1", actual);
	}

	@Test
	public void simpleTextLookupInThemeMessageSources() throws Exception {
		StaticMessageSource newMessageSource = new StaticMessageSource();
		ApplicationContextMessageProvider text = new ApplicationContextMessageProvider("Some text");

		PikesPeakTestRequest pikesPeakRequest = new PikesPeakTestRequest(newMessageSource, themeMessageSource,
				localeResolver, themeResolver);

		CharSequence actual = text.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertEquals("Translated Theme Text", actual);
	}
}
